﻿using System;
using System.Collections.Generic;
using ProjScan.Config;
using ProjScan.RecordManager;
using ProjScan.ScanRule;
using ProjScan.ScanRule.Attr;
using ProjScan.Submodules.Scene_Check;
using ProjScan.Tools;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace ProjScan.Modules
{
	// Token: 0x02000310 RID: 784
	internal class ScenesFixModule : BaseModules<ScenesFixModule, ScenesCheckConfig>
	{
		// Token: 0x06001281 RID: 4737 RVA: 0x00057604 File Offset: 0x00055804
		public override void Fix()
		{
			FixLog.Instance.Log(Module.Scenes_Fix);
			Singleton<PerformanceMeasurer>.Instance.PushState("SceneRuleFix");
			this.Init();
			this.LoadRules(LoadMode.FixModules);
			this.LoadFixers();
			bool flag = this.baseRules_Fix == null || this.baseRules_Fix.Count == 0;
			if (flag)
			{
				ScanLog.Instance.LogInfo(Module.Basic_Assets_Check, ScanLog.Stage.s2, "Rules is empty");
				ScanLog.UnityLogError("场景修复运行失败：相关规则全部关闭");
				this.Close();
			}
			else
			{
				Scene scene = default(Scene);
				int i = 0;
				while (i < ScenesMapData.Instance.scenesList.Length)
				{
					try
					{
						scene = ScenesMapData.Instance.LoadScene(ScenesMapData.Instance.scenesList[i]);
					}
					catch (Exception ex)
					{
						ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Unknown, ScanLog.Stage.s3, "Open Scene", string.Concat(new string[]
						{
							(ex.GetType() == null) ? null : ex.GetType().ToString(),
							"\n",
							ex.Message,
							"\n",
							ex.StackTrace
						}));
						Debug.LogError("Proj Scan提示：场景修复失败 - 无法打开场景 @" + ScenesMapData.Instance.scenesList[i] + "。 请检查该场景是否存在。");
						goto IL_278;
					}
					goto IL_132;
					IL_278:
					i++;
					continue;
					IL_132:
					this.InitRules(1000 + i * this.baseRules_Fix.Count);
					for (int j = 0; j < this.recordRules_Fix.Count; j++)
					{
						RecordRule recordRule = this.recordRules_Fix[j];
						recordRule.SceneId = new int?(ScenesMapData.Instance.Index);
						recordRule.SceneName = ScenesMapData.Instance.scenesList[i];
						recordRule.FixRule();
						string fullName = recordRule.GetType().FullName;
						FailedResultManager.Instance.UpdateFailedData(fullName, recordRule.failAssetInfos);
					}
					foreach (GlobalRule globalRule in this.globalRules_Fix)
					{
						globalRule.SceneId = new int?(ScenesMapData.Instance.Index);
						globalRule.SceneName = ScenesMapData.Instance.scenesList[i];
						globalRule.FixRule();
						string fullName2 = globalRule.GetType().FullName;
						FailedResultManager.Instance.UpdateFailedData(fullName2, globalRule.failAssetInfos);
					}
					ScenesMapData.Instance.SaveScene(scene, ScenesMapData.Instance.scenesList[i]);
					goto IL_278;
				}
				this.Close();
			}
		}

		// Token: 0x06001282 RID: 4738 RVA: 0x000578CC File Offset: 0x00055ACC
		public override void Init()
		{
			this.config = (Singleton<ConfigMgr>.Instance.GetConfig<ScenesCheckConfig>() as ScenesCheckConfig);
			ScenesMapData.Instance.Process();
			RecordManagerFactory.Instance.Init();
			this.baseRules_Fix.Clear();
			this.recordRules_Fix.Clear();
			this.globalRules_Fix.Clear();
			this.ruleAttr = typeof(ScenesCheckRuleAttr);
		}

		// Token: 0x06001283 RID: 4739 RVA: 0x0005793C File Offset: 0x00055B3C
		public override void Close()
		{
			ToolsHelper.ClearProgressBar();
			FailedResultManager.Instance.SaveAll();
			Singleton<PerformanceMeasurer>.Instance.CloseAndRecordState();
			Singleton<PerformanceMeasurer>.Instance.AddExecutionCheckpoint("ScenesFixEnd");
			ScanLog.Instance.LogInfo(Module.Scenes_Check, ScanLog.Stage.s6, "Done.");
		}

		// Token: 0x06001284 RID: 4740 RVA: 0x00057988 File Offset: 0x00055B88
		public override void FixSingle(string string_0)
		{
			FixLog.Instance.OpenFixLog(false);
			FixLog.Instance.Log(Module.Scenes_Fix);
			Singleton<PerformanceMeasurer>.Instance.PushState("SceneSingleRuleFix");
			this.Init();
			this.LoadRules(LoadMode.FixSingle, string_0);
			this.LoadFixers();
			bool flag = this.baseRules_Fix == null || this.baseRules_Fix.Count == 0;
			if (flag)
			{
				ScanLog.Instance.LogInfo(Module.Basic_Assets_Check, ScanLog.Stage.s2, "Rules is empty");
				ScanLog.UnityLogError("场景修复运行失败：相关规则全部关闭");
				this.Close();
			}
			else
			{
				for (int i = 0; i < this.recordRules_Fix.Count; i++)
				{
					Scene scene = default(Scene);
					RecordRule recordRule = this.recordRules_Fix[i];
					bool flag2 = recordRule.failAssetInfos.Count != 0;
					if (flag2)
					{
						using (Dictionary<string, FailedAssetInfo>.Enumerator enumerator = recordRule.failAssetInfos.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								char[] separator = new char[]
								{
									'$'
								};
								KeyValuePair<string, FailedAssetInfo> keyValuePair = enumerator.Current;
								string text = keyValuePair.Key.Split(separator)[0];
								try
								{
									scene = ScenesMapData.Instance.LoadScene(text);
								}
								catch (Exception ex)
								{
									ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Unknown, ScanLog.Stage.s3, "Open Scene", string.Concat(new string[]
									{
										(ex.GetType() == null) ? null : ex.GetType().ToString(),
										"\n",
										ex.Message,
										"\n",
										ex.StackTrace
									}));
									Debug.LogError("Proj Scan提示：场景修复失败 - 无法打开场景 @" + text + "。 请检查该场景是否存在。");
									continue;
								}
								recordRule.SceneId = new int?(ScenesMapData.Instance.Index);
								recordRule.SceneName = text;
								recordRule.FixRule();
								ScenesMapData.Instance.SaveScene(scene, text);
								this.UpdateFailedData(recordRule);
							}
						}
					}
				}
				for (int j = 0; j < this.globalRules_Fix.Count; j++)
				{
					Scene scene2 = default(Scene);
					GlobalRule globalRule = this.globalRules_Fix[j];
					bool flag3 = globalRule.failAssetInfos != null && globalRule.failAssetInfos.Count != 0;
					if (flag3)
					{
						List<string> list = new List<string>(globalRule.failAssetInfos.Keys);
						foreach (string text2 in list)
						{
							char[] separator2 = new char[]
							{
								'$'
							};
							string text3 = text2.Split(separator2)[0];
							try
							{
								scene2 = ScenesMapData.Instance.LoadScene(text3);
							}
							catch (Exception ex2)
							{
								ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Unknown, ScanLog.Stage.s3, "Open Scene", string.Concat(new string[]
								{
									(ex2.GetType() == null) ? null : ex2.GetType().ToString(),
									"\n",
									ex2.Message,
									"\n",
									ex2.StackTrace
								}));
								Debug.LogError("Proj Scan提示：场景修复失败 - 无法打开场景 @" + text3 + "。 请检查该场景是否存在。");
								continue;
							}
							globalRule.SceneId = new int?(ScenesMapData.Instance.Index);
							globalRule.SceneName = text3;
							globalRule.FixRule();
							ScenesMapData.Instance.SaveScene(scene2, text3);
							this.UpdateFailedData(globalRule);
						}
						FailedResultManager.Instance.UpdateFailedData(string_0, globalRule.failAssetInfos);
					}
				}
				this.Close();
			}
		}
	}
}
